<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>工厂方法</title>
    <script>
        /* 工厂方法 */
        //创建一个对象
        /* var obj = {
            name: "孙悟空",
            age: 19,
            gender: "男",
            sayName: function() {
                console.log(this.name);
            }
        };
        var obj1 = {
            name: "沙和尚",
            age: 29,
            gender: "男",
            sayName: function() {
                console.log(this.name);
            }
        };
        var obj2 = {
            name: "猪八戒",
            age: 39,
            gender: "男",
            sayName: function() {
                console.log(this.name);
            }
        };
        obj.sayName();
        obj1.sayName();
        obj2.sayName(); */


        /*
         使用工厂方法创建对象
            通过该方法可以批量的创建对象
            使用工厂方法创建的对象，使用的构造函数都是Object
            所以创建的对象都是object类型
            导致无法区分出多种不同类型的对象
        */
        function createPertion(name, age, gender) {
            //创建一个新的对象
            var obj = new Object();
            obj.name = name;
            obj.age = age;
            obj.gender = gender;
            obj.sayName = function() {
                console.log(this.name);
            }

            //将新的对象返回
            return obj;
        }

        var obj2 = createPertion("wun", 34, "男");
        var obj3 = createPertion("hello", 34, "female")
        console.log(obj2);
        console.log(obj3);
        obj2.sayName();
        obj3.sayName();
    </script>
</head>

<body>

</body>

</html>